#ifndef __TOYM__BASE__ASYNC_LOGGER_H__
#define __TOYM__BASE__ASYNC_LOGGER_H__

#include "Base/Singleton.h"
#include "Base/FixedBuffer.h"
#include "Base/File.h"
#include <mutex>
#include <condition_variable>
#include <cstdint>
#include <chrono>
#include <thread>
#include <atomic>

namespace toym
{

class AsyncLogger: public Singleton<AsyncLogger> {
public:

    static
    void putLog(const char* log, std::size_t sz);

    static
    void flushLog();

    // static
    // AsyncLogger* getInstance() {

    // }

    void setProcessName(const char *name);

    void startThread();

    AsyncLogger();
private:

    void putLogNonstatic(const char* log, std::size_t sz);
    void flushLogNonstatic();

    void threadFunc();
    
    void writeToFile(BufferVertor& bv);

    const char* getLogFileName();

    BufferPtr m_curBuf;
    BufferPtr m_nextBuf;
    BufferVertor m_buffers;

    std::mutex m_mutex;
    std::condition_variable m_cond;
    std::chrono::seconds m_interval;

    File m_outputFile;
    std::size_t m_logFileSizeLimit;

    std::thread* m_thread;

    struct tm m_tm;

    char m_processName[128];
    char m_hostName[64];
    char m_logFileName[256];

    // static std::mutex s_SingletonMutex;
    // static std::atomic<AsyncLogger*> s_pInstance;
    
};

} // namespace toym


#endif //__TOYM__BASE__ASYNC_LOGGER_H__